﻿import QtQuick 2.0

import SerialDataitem 1.0
import PcfDataitem 1.0
import GpioDataitem 1.0
import HanldDataitem 1.0
import PrintLogitem 1.0
import ConfigFileitem 1.0

Rectangle{
    id:homePage

    color: "#F2F2F2"


    property bool  currentMachineCheck: true

    property alias  currentHandlStatus: currentHandlBtn.enabled
    property bool  isCurrentInit: false

    property bool currtentHandlStatus:false;//以前usb连接状态 现在无用

    property bool currtentOpenSerielStatus:false;
    property int currtentOpenSerielError:8;

    property int currtentMacheTempetureMin:24;
    property int currtentMacheTempetureMax:26;
    property int contentHeights:homePage.height - 160

    property int currtentMacheTempeture:0;
    property int currtentMacheAdc:100;
    property bool isCurrtentMacheAdc:configFileitem.getPowers() >= 1? true:false;

    property int currtentMacheNums:0; //当前运行状态的计数个数
    property bool isInitNumbers :false

    property int openTimes: configFileitem.getOpenTime()

    property int currentLimitTimes: configFileitem.getLimitTime()
    property int currentLimitDevices: configFileitem.getLimitDevice()
    property bool currentLimitStatuses: false


    signal sigExhaleClick();
    signal sigStandardClick();

    function handLimtData(){
        currentLimitStatuses = true
        if(currentLimitDevices > 0){
            if(gpioDataitem.getD1() == 1){
                gpioDataitem.closeD1();
            }
            if(gpioDataitem.getD4() == 1){
                gpioDataitem.closeD4();
            }
            if(gpioDataitem.getD2() == 0){
                gpioDataitem.openD2();
            }
            if(gpioDataitem.getD3() == 0){
                gpioDataitem.openD3();
            }

        }

        retTime.start()
        currentMachineCheck= true;
    }

    function handBackData(){
        currentLimitStatuses = false
        retTime.start()
        currentMachineCheck= true;
    }
    function handCurrentDate(){
        var currentDate = new Date();
        var date_tring = currentDate.toLocaleDateString();
        //  console.log("local = ",date_tring,currentDate.getFullYear(),currentDate.getDay())
        var curyear = parseInt(currentDate.getFullYear().toString());
        var curmonth = parseInt((currentDate.getMonth()+1).toString())
        var curday = parseInt(currentDate.getDate().toString())

        var curhours= parseInt(currentDate.getHours());
        var curminute= parseInt(currentDate.getMinutes());
        var cursec= parseInt(currentDate.getSeconds());

        var curhourstr="";
        if(curhours < 10){
            curhourstr="0"+curhours;
        }else{
            curhourstr=""+curhours;
        }
        var curminutestr="";
        if(curminute < 10){
            curminutestr="0"+curminute;
        }else{
            curminutestr=""+curminute;
        }

        var cursecstr="";
        if(cursec < 10){
            cursecstr="0"+cursec;
        }else{
            cursecstr=""+cursec;
        }

        dateRightText.text = curyear+"年"+curmonth+"月"+curday+"日" ;
        dateText.text = curhourstr+":"+curminutestr+":"+cursecstr;
    }

    function handSuccessBtn(){
        homePage.sigExhaleClick();
        //   hanldDataitem.readMcuData();
        if(currtentMacheNums > 0){
            currtentMacheNums--;
            hanldDataitem.setCurrentValue(currtentMacheNums+"");
        }
        isInitNumbers = true;
        printLogitem.printInfor("qml:dianji  kaishi ");
    }
    //顶部背景
    Rectangle{
        id:topBackground

        anchors.left: parent.left
        anchors.top: parent.top
        width: parent.width
        height: 276
        color: "#FFFFFF"
        Image {
            width: parent.width
            height: parent.height
            anchors.left: parent.left
            anchors.top: parent.top
            source: "qrc:/images/topbackgroundimage.png"
        }
        Text {
            id: topMacheneName
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 82
            width: parent.width
            height: 36
            color: "#FFFFFF"
            font.bold: true
            font.italic: true
            font.pixelSize: 33
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            text: qsTr("呼气分析仪")
        }
        Image {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 67
            anchors.leftMargin: 59
            width: 48
            height: 48
            source: "qrc:/images/personimages.png"
        }
        Image {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 72
            anchors.leftMargin: 64
            width: 38
            height: 38
            source: "qrc:/images/personinimages.png"
        }

        Text {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 67
            anchors.leftMargin: 110
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: 150
            height: 48
            color: "#FFFFFF"
            font.pixelSize: 22
            text: qsTr("Welcome")
        }

        Text {
            id: dateText
            anchors.left:  parent.left

            anchors.top: parent.top
            anchors.topMargin: 20
            height: 20
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            width: parent.width

            color: "#FFFFFF"
            font.pixelSize: 16
            text: qsTr("")
        }

        Text {
            id: dateRightText
            anchors.right:  parent.right
            anchors.rightMargin: 170
            anchors.top: parent.top
            anchors.topMargin: 20
            height: 20
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignRight
            width: 200
            color: "#FFFFFF"
            font.pixelSize: 16
            text: qsTr("")
        }

        Text {
            id: logText
            anchors.left:  parent.left
            anchors.leftMargin: 51
            anchors.top: parent.top
            anchors.topMargin: 116
            height: 25
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: logText.contentWidth
            color: "#FFFFFF"
            font.pixelSize: 20
            font.bold: true
            font.italic: true
            text:  qsTr("KAMYRON")+qsTr("  ")+qsTr("卡米龙")
        }

        Timer {
            id: dateTimer
            interval: 1000;
            running: true;
            repeat: true
            onTriggered:{
                handCurrentDate();
            }
        }

    }


    //中间背景
    Rectangle{
        id:midBackground
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.leftMargin: 30
        anchors.topMargin: 176
        anchors.rightMargin: 30
        anchors.bottomMargin: 130
        color: "#FFFFFF"
        radius: 25

        Rectangle{
            id:midInforBackground
            width: instrumentTipText.width + 50 + instrumentInforText.width+ equipmentTemperatureTipText.width+ equipmentTemperatureInforText.width + sensorTipText.width+ sensorInforText.width
            height: 40
            anchors.right:  parent.right
            anchors.top: parent.top
            anchors.rightMargin: 55
            anchors.topMargin: 40

            color: "#00000000"

            Item{
                id:midInforItem
                anchors.top: parent.top
                anchors.right:  parent.right
                height:40
                width: parent.width


                Rectangle{
                    width: instrumentTipText.width + 6
                    height: instrumentTipText.height
                    radius: 15
                    anchors.left: instrumentTipText.left
                    anchors.leftMargin: -3
                    anchors.top: instrumentTipText.top
                    color: "#F08924"

                }
                Text {
                    id: instrumentTipText
                    anchors.left:  parent.left
                    anchors.leftMargin: 20
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    height: 30
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: instrumentTipText.contentWidth
                    color: "#FFFFFF"
                    font.pixelSize: 20
                    text: qsTr("仪器自检:")
                }
                Text {
                    id: instrumentInforText
                    anchors.left:  instrumentTipText.right
                    anchors.leftMargin: 20
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    height: 30
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: instrumentInforText.contentWidth
                    color: "#303030"
                    font.pixelSize: 18
                    text: homePage.currentMachineCheck?qsTr("自检中"):qsTr("正常运行")
                }


                Rectangle{
                    width: equipmentTemperatureTipText.width + 6
                    height: equipmentTemperatureTipText.height
                    radius: 15
                    anchors.left: equipmentTemperatureTipText.left
                    anchors.leftMargin: -3
                    anchors.top: equipmentTemperatureTipText.top
                    color: "#7C60BB"

                }
                Text {
                    id: equipmentTemperatureTipText
                    anchors.left:  instrumentInforText.right
                    anchors.leftMargin: 20
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    height: 30
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: equipmentTemperatureTipText.contentWidth
                    color: "#FFFFFF"
                    font.pixelSize: 20
                    text: qsTr("仪器内温度:")
                }
                Text {
                    id: equipmentTemperatureInforText
                    anchors.left:  equipmentTemperatureTipText.right
                    anchors.leftMargin: 10
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    height: 30
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: equipmentTemperatureInforText.contentWidth
                    color: "#303030"
                    font.pixelSize: 18
                    text: (parseInt(currtentMacheTempeture/10))+"."+ (currtentMacheTempeture%10)+ qsTr("℃")
                }
                Rectangle{
                    width: sensorTipText.width + 6
                    height: sensorTipText.height
                    radius: 15
                    anchors.left: sensorTipText.left
                    anchors.leftMargin: -3
                    anchors.top: sensorTipText.top
                    color: "#6AB7F4"

                }
                Text {
                    id:sensorTipText
                    anchors.left:  equipmentTemperatureInforText.right
                    anchors.leftMargin: 20
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    height: 30
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: sensorTipText.contentWidth
                    color: "#FFFFFF"
                    font.pixelSize: 20
                    text: qsTr("传感器寿命:")
                }
                Text {
                    id: sensorInforText
                    anchors.left:  sensorTipText.right
                    anchors.leftMargin: 10
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    height: 30
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: sensorInforText.contentWidth
                    color: "#303030"
                    font.pixelSize: 18
                    text: currtentMacheNums+qsTr(" 次")
                }

            }


            Rectangle{
                id:standardBtn

                anchors.right:  parent.right
                anchors.rightMargin: 50
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 15
                width: 120
                height: 40
                visible: false
                radius: 20
                color:homePage.currentMachineCheck?("#5C38373A"):( currtentMacheNums > 0?(isInitNumbers ? (standardBtn.standardBtnPressed ? "#5C38373A":"#FF38373A" ):"#5C38373A"):"#5C38373A")

                property bool standardBtnPressed: false
                Text {
                    width: parent.width
                    height: parent.height
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                    font.pixelSize: 15
                    color: "#FDE1C4"
                    text: qsTr("肺泡气测试")
                }

                MouseArea{
                    anchors.fill: parent
                    enabled: !homePage.currentMachineCheck ? (currtentMacheNums > 0? (isInitNumbers ? true:false):false):false
                    onPressed: {
                        standardBtn.standardBtnPressed = true
                    }
                    onReleased: {
                        standardBtn.standardBtnPressed = false
                    }
                    onClicked: {
                        homePage.sigStandardClick();
                        isInitNumbers = false
                    }
                }

            }

        }
        //        ClockDial{
        //            id:clockDial
        //            anchors.left:  parent.left
        //            anchors.leftMargin: 30
        //            anchors.verticalCenter:parent.verticalCenter

        //        }

    }

    Rectangle{
        id:exhaleBtn



        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 39
        width: 250
        height: 50
        radius: 25
        color: homePage.currentMachineCheck ?("#5C24B9B7"):(currtentMacheNums > 0?(exhaleBtn.exhaleBtnPressed ? "#5C24B9B7":"#FF24B9B7"):"#5C24B9B7")

        property bool exhaleBtnPressed: false
        Text {
            width: parent.width
            height: parent.height
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: 30
            color: "#FFFFFF"
            text: qsTr("气体测量")
        }

        MouseArea{
            id:currentHandlBtn
            anchors.fill: parent
            enabled: !homePage.currentMachineCheck? (currtentMacheNums > 0? true:false):false
            onPressed: {
                exhaleBtn.exhaleBtnPressed = true
            }
            onReleased: {
                exhaleBtn.exhaleBtnPressed = false
            }
            onClicked: {
                homePage.sigExhaleClick();
                //   hanldDataitem.readMcuData();
                if(currtentMacheNums > 0){
                    currtentMacheNums--;
                    hanldDataitem.setCurrentValue(currtentMacheNums+"");
                }
                isInitNumbers = true;
                printLogitem.printInfor("qml:dianji  kaishi ");
            }
        }

    }



    SerialDataitem{
        id:serialDataitem
        onSigSendStatus: {

            currtentOpenSerielError  = status;
            console.log("serialDataitem currtentOpenSerielError=="+currtentOpenSerielError);
            if(currtentOpenSerielError == 8){

                //  currentMachineCheck = false


            }

        }
    }

    PcfDataitem{
        id:pcfDataitem
    }

    GpioDataitem{
        id:gpioDataitem
    }

    HanldDataitem{
        id:hanldDataitem
        onSigCurrentData: {
            //currtentMacheNums = nums;
        }
        onSigAddDevice: {
            console.log("---onSigAddDevice")
            //            currtentHandlStatus = hanldDataitem.initHandl();
            //            if(!currtentHandlStatus){
            //                currtentMacheNums = 0;
            //            }else{

            //                hanldDataitem.readCurrentData();

            //            }
            if(hanldDataitem.initHandl()){
                hanldDataitem.readMcuClearData();

            }

        }
        onSigAddCurrentData: {
            currtentMacheNums += nums;
            hanldDataitem.setCurrentValue(currtentMacheNums+"");
        }
        onSigRemoveDevice: {
            console.log("---onSigRemoveDevice")
            //            currtentHandlStatus = false;
            //            if(!currtentHandlStatus){
            //                currtentMacheNums = 0;
            //            }
        }
    }
    Timer{
        id:compnentInitTimer
        running: false
        repeat: false
        interval: 1000 * openTimes
        onTriggered: {
            compnentInitTimer.stop()
            if(gpioDataitem.getD1() == 1){
                gpioDataitem.closeD1();
            }
            if(gpioDataitem.getD2() == 1){
                gpioDataitem.closeD2();
            }
            if(gpioDataitem.getD3() == 1){
                gpioDataitem.closeD3();
            }
            if(gpioDataitem.getD4() == 1){
                gpioDataitem.closeD4();
            }
            currentMachineCheck = false
        }
    }

    Timer{
        id:compnentTimer

        running: false
        repeat: false
        interval: 1000
        onTriggered: {
            compnentTimer.stop();
            currtentOpenSerielStatus = serialDataitem.initSatus();

            //   currtentHandlStatus = hanldDataitem.initHandl();
            currtentMacheNums = hanldDataitem.getCurrentValue();
            currtentHandlStatus = true;

            if(hanldDataitem.initHandl()){
                // hanldDataitem.readCurrentData();
                hanldDataitem.readMcuClearData();


            }
            console.log("compnentTimer currtentOpenSerielStatus=="+currtentOpenSerielStatus);
            console.log("compnentTimer currtentHandlStatus=="+currtentHandlStatus);
            if(currtentOpenSerielStatus){
                if(openTimes > 0){
                    compnentInitTimer.start()
                    if(gpioDataitem.getD1() == 1){
                        gpioDataitem.closeD1();
                    }
                    if(gpioDataitem.getD4() == 1){
                        gpioDataitem.closeD4();
                    }
                    if(gpioDataitem.getD2() == 0){
                        gpioDataitem.openD2();
                    }
                    if(gpioDataitem.getD3() == 0){
                        gpioDataitem.openD3();
                    }
                }else{
                    currentMachineCheck = false
                }

            }

        }
    }
    Timer{
        id:retTime
        running: false
        repeat: false
        interval: currentLimitStatuses ?(currentLimitTimes *1000 ):(1000* 60 * 3)
        onTriggered: {
            if(currentLimitStatuses){
                if(currentLimitDevices > 0){
                    if(gpioDataitem.getD1() == 1){
                        gpioDataitem.closeD1();
                    }
                    if(gpioDataitem.getD2() == 1){
                        gpioDataitem.closeD2();
                    }
                    if(gpioDataitem.getD3() == 1){
                        gpioDataitem.closeD3();
                    }
                    if(gpioDataitem.getD4() == 1){
                        gpioDataitem.closeD4();
                    }
                }

            }else{
                retTime.stop();
            }
            currentLimitStatuses = false

            homePage.currentMachineCheck = false

        }
    }
    Timer{
        id:pcfDataitemTimer

        running: true
        repeat: true
        interval: 500
        onTriggered: {
            currtentMacheTempeture = pcfDataitem.readTempture();

            console.log("currtentMacheTempeture =="+currtentMacheTempeture)
            if(isCurrtentMacheAdc){
                currtentMacheAdc =  pcfDataitem.readAdc();
            }



            if(currtentMacheTempeture < currtentMacheTempetureMin * 10 ){
                if( gpioDataitem.getRefri() == 1){
                    gpioDataitem.closeRefri();
                }


                if( gpioDataitem.getHeat() == 0){
                    gpioDataitem.openHeat();
                }


                return;
            }

            if(currtentMacheTempeture > currtentMacheTempetureMax * 10 ){

                if( gpioDataitem.getHeat() == 1){
                    gpioDataitem.closeHeat();
                }
                if( gpioDataitem.getRefri() == 0){
                    gpioDataitem.openRefri();
                }


                //                if(currtentOpenSerielError == 8){


                //                    if(currtentHandlStatus ){
                //                        currentMachineCheck = false
                //                        // hanldDataitem.readCurrentData();
                //                    }

                //                }


                return;
            }

            if( currtentMacheTempetureMin * 10<= currtentMacheTempeture&& currtentMacheTempeture <= currtentMacheTempetureMax * 10){
                if( gpioDataitem.getHeat() == 1){
                    gpioDataitem.closeHeat();
                }
                if( gpioDataitem.getRefri() == 1){
                    gpioDataitem.closeRefri();
                }



                return;
            }

        }
    }
    PrintLogitem{
        id:printLogitem
    }

    ConfigFileitem{
        id:configFileitem
    }

    Component.onCompleted: {
        handCurrentDate();
        if(!isCurrentInit){
            compnentTimer.start();
            isCurrentInit = true;
        }
    }
}

